package com.demo.async;

/**
 * @author leiliang
 * @version 1.0
 * @Description
 * @date 2020/11/11 13:19
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;
/**
 * Asynchronous Tasks
 * @author Xu
 *
 */
@Component
@Slf4j
public class AsyncTask {

    @Async("mySimpleAsync")
    public Future<String> doTask1() throws InterruptedException{
        log.info("Task1 started.");
        log.info("doTask1当前运行的线程名称：" + Thread.currentThread().getName());

        long start = System.currentTimeMillis();
        Thread.sleep(5000);
        long end = System.currentTimeMillis();

        log.info("Task1 finished, time elapsed: {} ms.", end-start);

        return new AsyncResult<>("Task1 accomplished!");
    }

    @Async("myAsync")
    public Future<String> doTask2() throws InterruptedException{
        log.info("Task2 started.");
        log.info("doTask2当前运行的线程名称：" + Thread.currentThread().getName());

        long start = System.currentTimeMillis();
        Thread.sleep(3000);
        long end = System.currentTimeMillis();

        log.info("Task2 finished, time elapsed: {} ms.", end-start);

        return new AsyncResult<>("Task2 accomplished!");
    }
}